package com.bitbank.smartdevice.ui.base

import android.support.annotation.CallSuper
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.RecyclerView
import android.view.View
import com.bitbank.smartdevice.adapter.base.CommonAdapter
import com.bitbank.smartdevice.adapter.base.HeaderAndFooterWrapper


/**
 *  Date：2018/12/12
 *  Author：xulee
 *  Desc：通用list页面
 */
abstract class BaseListFragment<T> : BaseFragment() {

    private lateinit var mAdapter: HeaderAndFooterWrapper

    @CallSuper
    override fun initView(v: View) {

        var recyclerView = getRecycleView(v)

        var innerAdapter = getInnerAdapter()

        mAdapter = HeaderAndFooterWrapper(innerAdapter)

        var headerView = getHeaderView()
        if (null != headerView) mAdapter.addHeaderView(headerView)

        var footerView = getFooterView()
        if (null != footerView) mAdapter.addFootView(footerView)

        recyclerView.adapter = mAdapter

        var mRefreshLayout = getRefreshLayout()
        mRefreshLayout?.setOnRefreshListener { loadData() }
    }

    open fun getRefreshLayout(): SwipeRefreshLayout? {
        return null
    }

    abstract fun getRecycleView(v: View): RecyclerView
    abstract fun getInnerAdapter(): CommonAdapter<T>
    abstract fun loadData()

    open fun getHeaderView(): View? {
        return null
    }

    open fun getFooterView(): View? {
        return null
    }

    fun getAdapter(): HeaderAndFooterWrapper = mAdapter
}